package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.lkt.common.util.Query;
import io.lkt.modules.sys.dao.BBrandInfoDao;
import io.lkt.modules.sys.dao.BUserBrandInfoDao;
import io.lkt.modules.sys.entity.BBrandInfoEntity;
import io.lkt.modules.sys.entity.BUserBrandInfoEntity;
import io.lkt.modules.sys.entity.SysUserGzhEntity;
import io.lkt.modules.sys.service.BUserBrandInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.function.Function;


@Service("bUserBrandInfoService")
public class BUserBrandInfoServiceImpl  implements BUserBrandInfoService {
    @Autowired
    private BUserBrandInfoDao bUserBrandInfoDao;

    @Autowired
    private BBrandInfoDao bBrandInfoDao;


    @Override
    public List<BUserBrandInfoEntity> queryList(Map<String, Object> map) {

        List<Integer> brandIds=null;
        if(map.get("userGzhList")!=null) {
            List<SysUserGzhEntity> userGzhList = (List<SysUserGzhEntity>) map.get("userGzhList");
            if (userGzhList != null && userGzhList.size() == 1 && userGzhList.get(0).getGzhCode().equals("prodgjhkpt")) {//共建获客平台
                brandIds=new ArrayList<>();

                brandIds.add(41018);//共建集体
                brandIds.add(35456);//科技双特
                brandIds.add(27235);//聚多荣创
                brandIds.add(10840);//高佣联盟
                brandIds.add(52232);//黑卡优选
                map.put("brandIds", brandIds);
            }
        }

        return bUserBrandInfoDao.queryList(map);
    }

    @Override
    public Integer queryTotal(Map<String, Object> map) {
        return bUserBrandInfoDao.queryTotal(map);
    }

    @Override
    public Integer vipTotal(Integer brand_id) {
        return bUserBrandInfoDao.vipTotal(brand_id);
    }

    @Override
    public Map<String, Object> articleTotal(Integer brand_id) {
        return bUserBrandInfoDao.articleTotal(brand_id);
    }

    @Override
    public Integer tencentTotal(Integer brand_id) {
        return bUserBrandInfoDao.tencentTotal(brand_id);
    }

    @Override
    public List<BUserBrandInfoEntity> queryUser(Map<String, Object> map) {
        return bUserBrandInfoDao.queryUser(map);
    }

    @Override
    public Integer queryUserTotal(Map<String, Object> map) {
        return bUserBrandInfoDao.queryUserTotal(map);
    }

    @Override
    public Integer mergeBrand(BUserBrandInfoEntity bUserBrandInfoEntity) {
        return bUserBrandInfoDao.mergeBrand(bUserBrandInfoEntity);
    }

    @Override
    public Integer delUserBrand(Integer brandId) {
        return bUserBrandInfoDao.delUserBrand(brandId);
    }

    @Override
    public boolean save(BUserBrandInfoEntity entity) {
        return false;
    }

    @Override
    public boolean saveBatch(Collection<BUserBrandInfoEntity> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<BUserBrandInfoEntity> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean removeById(Serializable id) {
        return false;
    }

    @Override
    public boolean removeByMap(Map<String, Object> columnMap) {
        return false;
    }

    @Override
    public boolean remove(Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return false;
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return false;
    }

    @Override
    public boolean updateById(BUserBrandInfoEntity entity) {
        return false;
    }

    @Override
    public boolean update(BUserBrandInfoEntity entity, Wrapper<BUserBrandInfoEntity> updateWrapper) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<BUserBrandInfoEntity> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(BUserBrandInfoEntity entity) {
        return false;
    }

    @Override
    public BUserBrandInfoEntity getById(Serializable id) {
        return null;
    }

    @Override
    public Collection<BUserBrandInfoEntity> listByIds(Collection<? extends Serializable> idList) {
        return null;
    }

    @Override
    public Collection<BUserBrandInfoEntity> listByMap(Map<String, Object> columnMap) {
        return null;
    }

    @Override
    public BUserBrandInfoEntity getOne(Wrapper<BUserBrandInfoEntity> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return null;
    }

    @Override
    public int count(Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return 0;
    }

    @Override
    public List<BUserBrandInfoEntity> list(Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return null;
    }

    @Override
    public IPage<BUserBrandInfoEntity> page(IPage<BUserBrandInfoEntity> page, Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return null;
    }

    @Override
    public List<Map<String, Object>> listMaps(Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return null;
    }

    @Override
    public <V> List<V> listObjs(Wrapper<BUserBrandInfoEntity> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public IPage<Map<String, Object>> pageMaps(IPage<BUserBrandInfoEntity> page, Wrapper<BUserBrandInfoEntity> queryWrapper) {
        return null;
    }

    @Override
    public BaseMapper<BUserBrandInfoEntity> getBaseMapper() {
        return null;
    }


    @Override
    public int storeUserSubBrand(Integer userId, String brandName) {

        //先将用户当前所有品牌设置为无效
        bUserBrandInfoDao.updateUserBrandEffectiveStatus(userId);

        if(brandName!=null && !brandName.trim().equals("")) {
            String[] branNames = brandName.split(",");

            Integer index=0;
            for (String brandNameInfo : branNames) {
                BBrandInfoEntity bBrandInfoEntity=bBrandInfoDao.queryByName(brandNameInfo);
                if(bBrandInfoEntity!=null) {
                    Integer brandIdInt = bBrandInfoEntity.getId();

                    //查询用户对当前品牌的设置情况
                    BUserBrandInfoEntity queryInfo = new BUserBrandInfoEntity();
                    queryInfo.setUserId(userId);
                    queryInfo.setBrandId(brandIdInt);
                    BUserBrandInfoEntity brandStatusInfo = bUserBrandInfoDao.queryUserBrandStatusInfo(queryInfo);

                    //如果不存在，则直接添加
                    if (brandStatusInfo == null) {
                        BUserBrandInfoEntity addBrandInfo = new BUserBrandInfoEntity();
                        addBrandInfo.setBrandId(brandIdInt);
                        addBrandInfo.setUserId(userId);
                        addBrandInfo.setBrandStatus(0);
                        addBrandInfo.setEffectiveStatus(1);
                        addBrandInfo.setCreateTime(new Date());
                        addBrandInfo.setIndexNum(index);
                        bUserBrandInfoDao.addUserBrandInfo(addBrandInfo);
                    } else {
                        //如果品牌为无效状态，则进行修改
                        int effectiveStatus = Optional.ofNullable(brandStatusInfo.getEffectiveStatus()).orElse(0);
                        if (effectiveStatus == 0) {
                            BUserBrandInfoEntity updateBrandInfo = new BUserBrandInfoEntity();
                            updateBrandInfo.setId(brandStatusInfo.getId());
                            updateBrandInfo.setEffectiveStatus(1);
                            updateBrandInfo.setModifyTime(new Date());
                            updateBrandInfo.setIndexNum(index);
                            bUserBrandInfoDao.updateUserBrandInfo(updateBrandInfo);
                        }

                    }
                }
                index++;
            }
            processUserBrandSelectStatus(userId);
        }
        return 0;
    }

    @Override
    public Integer getUserBrandCount(Integer userId) {
        return bUserBrandInfoDao.getUserBrandCount(userId);
    }

    @Override
    public List<BUserBrandInfoEntity> getUserBrand(Map<String, Object> map) {
        return bUserBrandInfoDao.getUserBrand(map);
    }

    @Override
    public BUserBrandInfoEntity getSimplyInfoById(Integer id) {
        return bUserBrandInfoDao.getSimplyInfoById(id);
    }

    @Override
    public int updateUserBrandPoster(Integer id, String brandPoster) {
        return bUserBrandInfoDao.updateUserBrandPoster(id, brandPoster);
    }

    @Override
    public BBrandInfoEntity getUserLatestBrandInfo(Integer userId) {
        return bUserBrandInfoDao.getUserLatestBrandInfo(userId);
    }

    /**
     * 处理微站品牌选中信息
     * @param userId
     */
    private void processUserBrandSelectStatus(Integer userId) {

        List<BUserBrandInfoEntity> userBrandCombinedInfoList = bUserBrandInfoDao.queryUserBrandInfo(userId);

        if (CollectionUtils.isNotEmpty(userBrandCombinedInfoList)) {

            boolean matchResult = userBrandCombinedInfoList.stream().parallel().noneMatch(info -> Optional.ofNullable(info.getBrandStatus()).orElse(0).intValue() == 1);

            //如果找不到选中的品牌信息，则设置第一个为选中
            if (matchResult) {

                BUserBrandInfoEntity userBrandInfo = new BUserBrandInfoEntity();
                userBrandInfo.setUserId(userId);
                Integer brandId = userBrandCombinedInfoList.get(0).getBrandId();
                userBrandInfo.setBrandId(brandId);
                bUserBrandInfoDao.updateUserSpecialBrandStatus(userBrandInfo);

            }

//            BrandInfo selectBrandInfo = userBrandInfoMapper.queryUserSelectBrandInfo(userId);
//
//            if (selectBrandInfo != null && selectBrandInfo.getId() != null) {
//                addUserPlatformCommodityAsyncTask.addUserPlatformCommodityAsyncTask(userId, selectBrandInfo.getId());
//            }
        }

    }
}
